package com.icongyou.enterprise.talent_analysis.mapper;

import com.icongyou.enterprise.talent_analysis.entity.PortraitTemplate;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PortraitTemplateMapper {

    @Insert("INSERT INTO portrait_template (id, enterprise_id, template_name, description, dimensions, " +
            "is_active, ai_suggested, created_by, created_time, updated_time) " +
            "VALUES (#{id}, #{enterpriseId}, #{templateName}, #{description}, #{dimensionsJson}, " +
            "#{active}, #{aiSuggested}, #{createdBy}, #{createdAt}, #{updatedAt})")
    int insert(PortraitTemplate template);

    @Select("SELECT id, enterprise_id, template_name, description, dimensions as dimensionsJson, " +
            "is_active as active, ai_suggested as aiSuggested, created_by, created_time as createdAt, " +
            "updated_time as updatedAt FROM portrait_template WHERE id = #{id}")
    PortraitTemplate selectById(@Param("id") String id);
    
    @Update("UPDATE portrait_template SET template_name = #{templateName}, description = #{description}, " +
            "dimensions = #{dimensionsJson}, is_active = #{active}, updated_time = #{updatedAt} " +
            "WHERE id = #{id}")
    int updateById(PortraitTemplate template);

    @Select("<script>" +
            "SELECT id, enterprise_id, template_name, description, dimensions as dimensionsJson, " +
            "is_active as active, ai_suggested as aiSuggested, created_by, created_time as createdAt, " +
            "updated_time as updatedAt FROM portrait_template WHERE enterprise_id = #{enterpriseId} " +
            "<if test='active != null'>AND is_active = #{active}</if>" +
            "</script>")
    List<PortraitTemplate> selectByEnterprise(@Param("enterpriseId") String enterpriseId,
                                              @Param("active") Boolean active);

    /**
     * 查询HR用户创建的模板数量
     */
    @Select("SELECT COUNT(*) FROM portrait_template WHERE created_by = #{createdBy}")
    int countByCreatedBy(@Param("createdBy") String createdBy);

    @Update("UPDATE portrait_template SET is_active = 0 WHERE enterprise_id = #{enterpriseId}")
    int clearActiveFlag(@Param("enterpriseId") String enterpriseId);
    
    @Delete("DELETE FROM portrait_template WHERE id = #{id}")
    int deleteById(@Param("id") String id);
}
